{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "58e454ac-9062-4d26-9aef-aa45c7d2d3e0",
   "metadata": {},
   "source": [
    "### 饼图"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ea1b9086-517d-4636-99dc-103b6f8e0e49",
   "metadata": {},
   "outputs": [],
   "source": [
    "from pyecharts import options as opts\n",
    "from pyecharts.charts import Page, Pie\n",
    "\n",
    "pic_name = 'Pyecharts-render.html'\n",
    "name = ['草莓', '芒果', '香蕉', '雪梨', '西瓜', '柠檬', '葡萄']\n",
    "title_opts={'text': 'Pie水果','subtext':'--销售比例'}\n",
    "value = [23,32,12,13,10,24,56]\n",
    "data = [tuple(z) for z in zip(name, value)]\n",
    "pie = Pie().add('', data).set_global_opts() # pie数据为二元组或二元素数组的数组或元组\n",
    "pie.render(pic_name)\n",
    "# pie.render_notebook()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "238c0397-7a28-48de-94e4-23a468bd2acd",
   "metadata": {},
   "outputs": [],
   "source": [
    "pie.set_global_opts(title_opts=opts.TitleOpts(title='Pie', subtitle='水果'))\n",
    "pie.set_colors(['blue', 'green', 'yellow', 'red', 'pink', 'orange', 'purple'])\n",
    "pie.render(pic_name)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "61b7eb0b-8805-4cec-864c-22e2318888e9",
   "metadata": {},
   "source": [
    "### 漏斗图"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "25719909-5c19-453d-8570-7ab04ec8e4f9",
   "metadata": {},
   "outputs": [],
   "source": [
    "from pyecharts.charts import Funnel, Page\n",
    "\n",
    "funnel = Funnel().add('商品', data).set_series_opts(label_opts=opts.LabelOpts(formatter='{b}: {c}'))\n",
    "funnel.render(pic_name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1fc34808-a587-458f-8afd-5f731f80774d",
   "metadata": {},
   "outputs": [],
   "source": [
    "funnel = (Funnel()\n",
    "          .add('商品', data, label_opts=opts.LabelOpts(formatter='{b}: {c}', position='inside'), sort_='ascending')\n",
    "          .set_global_opts(title_opts=title_opts))\n",
    "funnel.render(pic_name)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e4f877e0-d0bf-494a-9238-2187671e42c8",
   "metadata": {},
   "source": [
    "### 仪表盘Gauge"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "84c01127-a125-45e8-a8c1-9d4ab25ad2cf",
   "metadata": {},
   "outputs": [],
   "source": [
    "from pyecharts.charts import Gauge\n",
    "from pyecharts.globals import ThemeType\n",
    "\n",
    "init_opts = opts.InitOpts(theme=ThemeType.DARK)  # 后续再验证\n",
    "gd = [('完成率', 66.9)]\n",
    "gauge = Gauge(init_opts=init_opts).add('gauge 示例', gd).set_global_opts(title_opts={'title': '仪表盘'})\n",
    "gauge.render(pic_name)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f6396fdf-5a8a-417f-be14-103cc283eef9",
   "metadata": {},
   "source": [
    "### 关系图"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "89606087-d4e5-40b8-8da1-f658a38de2e2",
   "metadata": {},
   "outputs": [],
   "source": [
    "import json\n",
    "import os\n",
    "from pyecharts.charts import Graph\n",
    "\n",
    "nodes = [\n",
    "    {'name': '玄武', 'symbolSize': 10},\n",
    "    {'name': '朱雀', 'symbolSize': 50},\n",
    "    {'name': '青龙', 'symbolSize': 20},\n",
    "    {'name': '白虎', 'symbolSize': 30},\n",
    "    {'name': '四兽', 'symbolSize': 10}\n",
    "]\n",
    "links = []\n",
    "for i in nodes:\n",
    "    for j in nodes:\n",
    "        links.append({'source': i['name'], 'target': j['name']})\n",
    "\n",
    "graph = (Graph(init_opts=opts.InitOpts(theme=ThemeType.MACARONS))\n",
    "         .add('', nodes, links, repulsion=8000, label_opts=opts.LabelOpts(formatter='{b}', position='inside'))\n",
    "         .set_global_opts(title_opts={'title': '关系图'}))\n",
    "graph.render(pic_name)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "198ccd45-99b3-4628-989c-326c4759b7bb",
   "metadata": {},
   "source": [
    "### 词云"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "96b908b5-ed5f-44c6-b781-ce07f43be129",
   "metadata": {},
   "outputs": [],
   "source": [
    "from pyecharts.charts import WordCloud\n",
    "from pyecharts.globals import SymbolType\n",
    "\n",
    "words = [('修仙', 9000), ('炼丹术', 8650), ('炼器术', 5008), ('血脉师', 8105), ('阵法师', 5013), \n",
    "         ('北域大陆', 4251), ('白朝之地', 6542), ('五国', 3655), ('上古时代', 2154), ('千年以前', 4562)]\n",
    "wc = WordCloud().add('', words, word_size_range=[10, 50]).set_global_opts(title_opts={'title': '词云'})\n",
    "wc.render(pic_name)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aaf3ace7-470d-4a2d-b1f9-43c20aaf6527",
   "metadata": {},
   "source": [
    "### 坐标系图表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6a95acf1-5544-4121-9cda-05ae9186ee91",
   "metadata": {},
   "outputs": [],
   "source": [
    "from pyecharts.charts import Bar\n",
    "\n",
    "columns = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']\n",
    "y1 = [2.0, 4.9, 7.0, 23.2, 25.6, 76.7, 135.6, 162.2, 32.6, 20.0, 6.4, 3.3]\n",
    "y2 = [2.6, 5.9, 9.0, 26.4, 28.7, 70.7, 175.6, 182.2, 48.7, 18.8, 6.0, 2.3]\n",
    "bar = (Bar().add_xaxis(columns)\n",
    "      .add_yaxis('降水量', y1)\n",
    "      .add_yaxis('蒸发量', y2)\n",
    "      .set_series_opts(markline_opts=opts.MarkLineOpts(data=[opts.MarkLineItem(type_='average', name='平均值')]))\n",
    "      .set_series_opts(markpoint_opts=opts.MarkPointOpts(data=[opts.MarkPointItem(type_='min', name='最小值'),\n",
    "                                                               opts.MarkPointItem(type_='max', name='最大值')]))\n",
    "      .set_global_opts(title_opts=opts.TitleOpts(title='年降水量和蒸发量')))\n",
    "bar.render(pic_name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4e65cf9b-5c25-4463-bfbe-f4859117850b",
   "metadata": {},
   "outputs": [],
   "source": [
    "from pyecharts.charts import Line\n",
    "x = ['橙花醇', '香叶醇', '苯乙醇', '紫罗兰酮', '异茉莉酮', '香茅醇', '佳乐麝香']\n",
    "tb = [5, 20, 36, 10, 75, 90, 52]\n",
    "jd = [9, 21, 35, 15, 70, 85, 50]\n",
    "line = (Line()\n",
    "        .add_xaxis(x)\n",
    "        .add_yaxis('淘宝', tb, is_smooth=True) # 平使用滑曲线\n",
    "        .add_yaxis('JD', jd)\n",
    "        .set_global_opts(title_opts=opts.TitleOpts(title='商铺存货情况', subtitle='香料'), \n",
    "                        toolbox_opts=opts.ToolboxOpts(),\n",
    "                        legend_opts=opts.LegendOpts(is_show=True))\n",
    "        .set_series_opts(markline_opts=opts.MarkLineOpts(data=[opts.MarkLineItem(type_='average', name='平均值')])))\n",
    "line.render(pic_name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "39a3355f-4a26-42e0-8f24-fd817f2771ef",
   "metadata": {},
   "outputs": [],
   "source": [
    "from pyecharts.charts import Scatter\n",
    "\n",
    "sc = (Scatter()\n",
    "        .add_xaxis(x)\n",
    "        .add_yaxis('淘宝', tb)\n",
    "        .set_global_opts(title_opts=opts.TitleOpts(title='商铺存货情况', subtitle='香料'), \n",
    "                        legend_opts=opts.LegendOpts(is_show=True)))\n",
    "sc.render(pic_name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "af7ae8a4-81ce-4d3f-9760-4338fbf85a6d",
   "metadata": {},
   "outputs": [],
   "source": [
    "bar = Bar().add_xaxis(x).add_yaxis('TB', tb) # , background_style=opts.BarBackgroundStyleOpts(opacity=70, color='rgba(201, 151, 180, 0.5)') 未实现\n",
    "line = (Line(init_opts=opts.InitOpts(theme=ThemeType.SHINE))\n",
    "        .add_xaxis(x)\n",
    "        .add_yaxis('JD', jd, markline_opts=opts.MarkPointOpts(data=[opts.MarkLineItem(type_='average')]))\n",
    "        .set_global_opts(title_opts=opts.TitleOpts(title='商铺存货情况')))\n",
    "bar.overlap(line)\n",
    "bar.render(pic_name)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d3361b1f-bf96-4261-b732-d636b43790ce",
   "metadata": {},
   "source": [
    "### 地图"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5af75495-6258-4c4a-b17a-659002690662",
   "metadata": {},
   "outputs": [],
   "source": [
    "from pyecharts.charts import Geo\n",
    "from pyecharts.globals import ChartType, SymbolType, GeoType\n",
    "\n",
    "provinces = ['广东', '安徽', '山西', '湖南', '浙江', '江苏']\n",
    "pro_value = [54, 98, 65, 45, 56, 78]\n",
    "pr_data = [list(z) for z in zip(provinces, pro_value)]\n",
    "\n",
    "geo = (Geo()\n",
    "       .add_schema(maptype='china')  # 地名参考: pyecharts.datasets.map_filenames.json 相关文件\n",
    "       .add('geo', pr_data, type_=ChartType.SCATTER)\n",
    "       .set_series_opts(label_opts=opts.LabelOpts(is_show=False))\n",
    "       .set_global_opts(visualmap_opts=opts.VisualMapOpts(is_piecewise=True), \n",
    "                        title_opts=opts.TitleOpts(title='新晋天才武者人数')))\n",
    "geo.render(pic_name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5996057c-788c-4822-8a0f-c26dc7cb01bb",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 扩展地名\n",
    "\n",
    "ah_data = [\n",
    "    ['安庆市', 54],  ['合肥市', 65],  ['六安市', 76],  ['马鞍山', 64],  \n",
    "    ['芜湖市', 35],  ['池州市', 35],  ['蚌埠市', 54],  ['淮北市', 34],  \n",
    "    ['淮南市', 56],  ['黄山市', 87],  ['阜阳市', 43],  ['滁州市', 65],  \n",
    "    ['宣城市', 47],  ['毫州市', 45],  ['宿州市', 23],  ['铜陵市', 49], ['潜山市', 900]\n",
    "]\n",
    "anhui = (Geo()\n",
    "         .add_schema(maptype='安徽')\n",
    "         .add_coordinate('潜山市', 116.53, 30.62) # 扩展地名和坐标\n",
    "         .add('geo', ah_data, type_=ChartType.HEATMAP)\n",
    "         .set_series_opts(label_opts=opts.LabelOpts(is_show=True))\n",
    "         .set_global_opts(visualmap_opts=opts.VisualMapOpts(is_piecewise=True), title_opts=opts.TitleOpts(title='新城加入'))\n",
    "        )\n",
    "\n",
    "anhui.render(pic_name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e86fcd19-1a2d-4f28-88e4-fa7f5a5b1b88",
   "metadata": {},
   "outputs": [],
   "source": [
    "from pyecharts.charts import Map\n",
    "\n",
    "mp = (Map()\n",
    "      .add('Map', pr_data, 'china')\n",
    "      .set_global_opts(title_opts=opts.TitleOpts(title='Map示例'),\n",
    "                       visualmap_opts=opts.VisualMapOpts(max_=200, is_piecewise=True)))\n",
    "mp.render(pic_name)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c7a81556-3155-4675-ae91-b90caaa1534b",
   "metadata": {},
   "source": [
    "### 3D图像"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f25baf0a-7e12-429e-b3ac-85364e2ddbfc",
   "metadata": {},
   "outputs": [],
   "source": [
    "from pyecharts.charts import Surface3D\n",
    "import math\n",
    "\n",
    "def suface_data():\n",
    "    for t0 in range(-60, 60, 1):\n",
    "        y = t0/60\n",
    "        for t1 in range(-60, 60, 1):\n",
    "            x = t1/60\n",
    "            if math.fabs(x) < 0.1 and math.fabs(y) < 0.1:\n",
    "                z = '-'\n",
    "            else:\n",
    "                z = math.sin(x * math.pi) * math.sin(y * math.pi)\n",
    "            yield [x, y, z]\n",
    "\n",
    "surf3d = (Surface3D()\n",
    "          .add('',\n",
    "               list(suface_data()),\n",
    "               xaxis3d_opts=opts.Axis3DOpts(type_='value'),\n",
    "               yaxis3d_opts=opts.Axis3DOpts(type_='value'),\n",
    "               grid3d_opts=opts.Grid3DOpts(width=100, height=100, depth=100))\n",
    "          .set_global_opts(title_opts=opts.TitleOpts(title='Surface3D-示例'),\n",
    "                          visualmap_opts=opts.VisualMapOpts(max_=3, min_=-3)))\n",
    "\n",
    "surf3d.render(pic_name)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "452c7c2a-0102-4635-a6b2-226af036cb02",
   "metadata": {},
   "source": [
    "### 网络图"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3c5daab4-e3a4-4c51-9c5b-3d6701c6759a",
   "metadata": {},
   "outputs": [],
   "source": [
    "import networkx as nx\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "# 无向图Graph三种创建方式 \n",
    "g = nx.Graph()  #空\n",
    "g1 = nx.Graph([(1,2), (2,3), (1,3)]) # 基于节点数组构建\n",
    "g2 = nx.path_graph(10) # 生成10个节点\n",
    "\n",
    "# 添加关系\n",
    "g.add_node(1)  # 添加1个节点\n",
    "g.add_edge(2,3)\n",
    "g.add_edge(3,2)\n",
    "g.add_nodes_from([3,4,5,6])\n",
    "g.add_edges_from([(3,5), (6,3), (6,7)])\n",
    "\n",
    "print('ndoes: ', g.nodes())\n",
    "print('edges: ', g.edges())\n",
    "print('eg size: ', g.number_of_edges())\n",
    "\n",
    "nx.draw(g, with_labels=True, font_color='#e6a961', node_size=600, pos=nx.circular_layout(g), \n",
    "        node_color='#cae661', edge_color='#71e661', font_weight='bold')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "732eb366-2028-406e-961b-5fc761aced3d",
   "metadata": {},
   "outputs": [],
   "source": [
    "dg = nx.path_graph(4, create_using=nx.DiGraph())  # 有向图DiGraph\n",
    "dg.add_edges_from([(7,8), (8,2)])\n",
    "nx.draw(dg, with_labels=True, font_color='#e6a961', node_size=600, pos=nx.circular_layout(dg), \n",
    "        node_color='#cae661', edge_color='#71e661', font_weight='bold')\n",
    "\n",
    "# plt.savefig('pic.png')\n",
    "# dg.to_undirected()  # 转换\n",
    "# g.to_directed\n",
    "plt.show()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
